package com.glorystone.ins.controller;

import com.glorystone.ins.common.JsonResult;
import com.glorystone.ins.common.common.DataTable;
import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.domain.Insurer;
import com.glorystone.ins.domain.SysUser;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.ISysRoleService;
import com.glorystone.ins.service.InsurerService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author:LiuSC
 * @Date:Created in 2018-03-13 13:49
 * @since: JDK 1.7
 * @Description:
 */
@Controller
@RequestMapping("/insurer")
public class InsurerController extends BaseController {

    @Autowired
    private InsurerService insurerService;

    @Autowired
    private ISysRoleService iSysRoleService;

    /*@RequestMapping(value = "/tolist", method = RequestMethod.GET)
    public String toInsurerList(){
        return "insurer/insurer_list";
    }*/

    @RequestMapping(value="/tolist")
    public ModelAndView toInsurerList(ModelAndView mv){
        //获取所有业务部人员
        List<SysUser> list = iSysRoleService.getUserListByDepartmentId(RoleTypeIdEnum.BUSSNESSAll.getValue());
        mv.addObject("list",list);
        mv.setViewName("insurer/insurer");
        return mv;
    }

    @RequestMapping(value="/queryTableData",method = RequestMethod.GET)
    @ResponseBody
    public DataTable<Insurer> queryTableData(HttpServletRequest request){
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        PageVo vo =  parametToPageVoData(request,Insurer.class,user);
        PageList<Insurer> list = insurerService.queryAllInsurerList(vo);
        DataTable data = resultToDataTable(list);
        return data;
    }

    @RequestMapping(value="/toadd")
    public ModelAndView toAdd(ModelAndView mv){
        //获取所有业务部人员
        List<SysUser> list = iSysRoleService.getUserListByDepartmentId(RoleTypeIdEnum.BUSSNESSAll.getValue());
        mv.addObject("list",list);
        mv.setViewName("insurer/insurer_add");
        return mv;
    }

    @RequestMapping(value="/toedit/{tid}")
    public @ResponseBody
    JsonResult toEdit(@PathVariable Long tid){
        //获取所有业务部人员
        List<SysUser> list = iSysRoleService.getUserListByDepartmentId(RoleTypeIdEnum.BUSSNESSAll.getValue());
        Insurer insurer = insurerService.getInsurerById(tid);
        return JsonResult.createSuccess(insurer);
    }

    @RequestMapping("/del/{tid}")
    public @ResponseBody
    JsonResult delInsurer(@PathVariable Long tid){
        if(tid != null){
            Insurer insurer = new Insurer();
            insurer.setTid(tid);
            insurer.setDelFlag(1);
            boolean result = insurerService.updateInsuer(insurer);
            if(result){
                return returnSuccess("操作成功！");
            }
        }
        return JsonResult.createErrorMsg("操作失败！");
    }

    @RequestMapping(value="/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> addInsurer(@ModelAttribute(value = "form") Insurer insurer){
        Map<String,String> resualtMap = Maps.newHashMap();
        resualtMap.put("type","E");
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Long id = user.getTid();
        boolean result = false;
        if(insurer.getTid() !=null && insurer.getTid() >0){
            insurer.setUpdateDate(LocalDateTime.now());
            insurer.setUpdateBy((long) id);
            result = insurerService.updateInsuer(insurer);
        }else{
            insurer.setCreateBy((long) id);
            insurer.setCreateDate(LocalDateTime.now());
            insurer.setDelFlag(0);
            result = insurerService.addInsuer(insurer);
        }
        if(result){
            resualtMap.put("type","S");
        }
        return resualtMap;
    }
}
